#include "tcl_usb.h"
#include "tcl_tv.h"
TCL_USB::TCL_USB()
{
   empty=0;
}


bool TCL_USB::is_empty()
{
    if(0==empty)
    {
        return true;
    }else
        return false;
}

void TCL_USB::Clear()
{
       Clear_MSTAR_ATV();
       Clear_MSTAR_DTV();
       Clear_MTK_ATV();
       Clear_MTK_DTV();
       empty=0;
       u4_count=0;
       read_info=false;
       write_info=false;
       solution=0;
       dtv_atv_flag=0;
       version=0;
}

TCL_USB::~TCL_USB()
 {
    Clear();
 }
/*
TCL_USB::TCL_USB(bool exist)
{
  ch_is_exist=exist;
}
TCL_USB::TCL_USB(bool exist, bool info)
{
    ch_is_exist=exist;
   read_info=info;
}
TCL_USB::TCL_USB(bool exist, bool info1, bool info2)
{
    ch_is_exist=exist;
    read_info=info1;
    write_info=info2;
}
*/
void TCL_USB::set_count(const uint num)
{
     u4_count=num;
}

void TCL_USB::set_dtv_atv_flag(const qint8 flag)
{
     dtv_atv_flag=flag;
}

void TCL_USB::set_solution(const qint8 solution)
{
      this->solution=solution;
}


bool TCL_USB::Bin_Header_Test(BIN_FILE_HEADER &bn_hd)
{
    for(int i=0;i<MAGIC_LEN;i++)
    {
        if(bn_hd.magic[i]!=HEADER_MAGIC[i])
        {
            return false;
        }
    }
    return true;
}

int TCL_USB::Read_MTK_Data(QDataStream &in,int dtv_atv_flag)
{

            switch(dtv_atv_flag)
             {
         case 0://仅ATV
                 {
          TCL_MTK_ATV_TV *mtk_atv_tv;
                    while(!in.atEnd())
                    {
              mtk_atv_tv=new TCL_MTK_ATV_TV;
              Q_CHECK_PTR(mtk_atv_tv);
                     mtk_atv_tv->Read_Data(in);
             q_mtk_atv_tv<<mtk_atv_tv;
             // q_mtk_atv_tv.append(mtk_atv_tv);
                    }
                    break;
                }
         case 1://仅DTV
                 {
          TCL_MTK_DTV_TV *mtk_dtv_tv;
                 while(!in.atEnd())
                    {
              //                        TCL_MTK_DTV_TV *mtk_dtv_tv;
              mtk_dtv_tv=new TCL_MTK_DTV_TV;
              Q_CHECK_PTR(mtk_dtv_tv);
                        //QVector<TCL_MTK_DTV_TV*>q_mtk_dtv_tv;
                        mtk_dtv_tv->Read_Data(in);
            q_mtk_dtv_tv<<mtk_dtv_tv;
            //                        q_mtk_dtv_tv.append(mtk_dtv_tv);
                    }
                 break;
             }
         case 2://DTV和ATV 共存，ATV写在文件前面
                 {
                       u4_count=file_header.dtv_atv_length;
                       TCL_MTK_ATV_TV *mtk_atv_tv;
                       TCL_MTK_DTV_TV *mtk_dtv_tv;
                       for(int i=0;i<u4_count;i++)
                       {
            mtk_atv_tv=new TCL_MTK_ATV_TV;
            Q_CHECK_PTR(mtk_atv_tv);
                        mtk_atv_tv->Read_Data(in) ;
            q_mtk_atv_tv<<mtk_atv_tv;
                        //q_mtk_atv_tv.append(mtk_atv_tv);
                       }
               while(!in.atEnd())
             {
               mtk_dtv_tv=new TCL_MTK_DTV_TV;
               Q_CHECK_PTR(mtk_dtv_tv);
               mtk_dtv_tv->Read_Data(in);
               q_mtk_dtv_tv<<mtk_dtv_tv;
               //q_mtk_dtv_tv.append(mtk_dtv_tv);
             }
               break;
                   }
         case 3://DTV和ATV共存，DTV写在文件前面
                 {
                  u4_count=file_header.dtv_atv_length;
          TCL_MTK_ATV_TV *mtk_atv_tv;
          TCL_MTK_DTV_TV *mtk_dtv_tv;
                  for(int i=0;i<u4_count;i++)
            {
              mtk_dtv_tv=new TCL_MTK_DTV_TV;
              Q_CHECK_PTR(mtk_dtv_tv);
              mtk_dtv_tv->Read_Data(in);
              q_mtk_dtv_tv<<mtk_dtv_tv;
              //q_mtk_dtv_tv.append(mtk_dtv_tv);
            }
          while(!in.atEnd())
            {
              mtk_atv_tv=new TCL_MTK_ATV_TV;
              Q_CHECK_PTR(mtk_atv_tv);
              mtk_atv_tv->Read_Data(in);
                      q_mtk_atv_tv<<mtk_atv_tv;
              //q_mtk_atv_tv.apped(mtk_atv_tv);
            }
          break;
              }
         default:
           qDebug("MTK方案中DTV和ATV标志出现错误\n");
           return 0;
           }
        return 1;
}


int TCL_USB::Write_MTK_Data(QDataStream &out,int dtv_atv_flag)
{
  switch(dtv_atv_flag)
    {
    case 0:
      {
    TCL_MTK_ATV_TV *mtk_atv_tv;
    for(int i=0;i<q_mtk_atv_tv.size();i++)
      {
        mtk_atv_tv=q_mtk_atv_tv.at(i);
        Q_CHECK_PTR(mtk_atv_tv);
        mtk_atv_tv->Write_Data(out);
      }
      }
      break;
    case 1:
      {
    TCL_MTK_DTV_TV *mtk_dtv_tv;
    for(int i=0;i<q_mtk_dtv_tv.size();i++)
      {
        mtk_dtv_tv=q_mtk_dtv_tv.at(i);
        Q_CHECK_PTR(mtk_dtv_tv);
        mtk_dtv_tv->Write_Data(out);
      }
      }
      break;
    case 2:
      {
    TCL_MTK_ATV_TV *mtk_atv_tv;
    TCL_MTK_DTV_TV *mtk_dtv_tv;
        for(int i=0;i<q_mtk_atv_tv.size();i++)
      {
            mtk_atv_tv=q_mtk_atv_tv.at(i);
            Q_CHECK_PTR(mtk_atv_tv);
        mtk_atv_tv->Write_Data(out);
      }
    for(int i=0;i<q_mtk_dtv_tv.size();i++)
      {
        mtk_dtv_tv=q_mtk_dtv_tv.at(i);
         Q_CHECK_PTR(mtk_dtv_tv);
        mtk_dtv_tv->Write_Data(out);
      }
      }break;
    case 3:
      TCL_MTK_DTV_TV *mtk_dtv_tv;
      TCL_MTK_ATV_TV *mtk_atv_tv;
      for(int i=0;i<q_mtk_dtv_tv.size();i++)
    {
      mtk_dtv_tv=q_mtk_dtv_tv.at(i);
       Q_CHECK_PTR(mtk_dtv_tv);
      mtk_dtv_tv->Write_Data(out);
    }
      for(int i=0;i<q_mtk_atv_tv.size();i++)
    {
      mtk_atv_tv=q_mtk_atv_tv.at(i);
      mtk_atv_tv->Write_Data(out);
    }
      break;
    default:
      Q_ASSERT_X(dtv_atv_flag>=0&&dtv_atv_flag<4,"In TCL_USB::Write_MTK_Data() Line 228","dtv_atv_falg error!");
    // qDebug("DTV和ATV标志位错误\n");
      return 0;
    }
  return 1;
}


int TCL_USB::Read_MSTAR_Data(QDataStream &in,int dtv_atv_flag)
{

  switch(dtv_atv_flag)
    {
    case 0:
      {

        while(!in.atEnd())
           {
             TCL_MSTAR_ATV_TV *mstar_atv_tv=mstar_atv_tv=new TCL_MSTAR_ATV_TV();
             Q_CHECK_PTR(mstar_atv_tv);
             mstar_atv_tv->Read_Data(in);
             q_mstar_atv_tv<<mstar_atv_tv;
           }
        break;
      }
     case 1:
      {
          TCL_MSTAR_DTV_TV *mstar_dtv_tv;
          while(!in.atEnd())
             {
               mstar_dtv_tv=new TCL_MSTAR_DTV_TV;
               Q_CHECK_PTR(mstar_dtv_tv);
               mstar_dtv_tv->Read_Data(in);
               q_mstar_dtv_tv<<mstar_dtv_tv;
              }
          break;
        }
     case 2:
      {
        u4_count=file_header.dtv_atv_length;
        TCL_MSTAR_DTV_TV *mstar_dtv_tv;
        for(int i=0;i<u4_count;i++)
            {
             TCL_MSTAR_ATV_TV* mstar_atv_tv=new TCL_MSTAR_ATV_TV();
             Q_CHECK_PTR(mstar_atv_tv);
             mstar_atv_tv->Read_Data(in) ;
             q_mstar_atv_tv<<mstar_atv_tv;
            }
               while(!in.atEnd())
             {
               mstar_dtv_tv=new TCL_MSTAR_DTV_TV;
               Q_CHECK_PTR(mstar_dtv_tv);
               mstar_dtv_tv->Read_Data(in);
               q_mstar_dtv_tv<<mstar_dtv_tv;
               //q_mstar_dtv_tv.append(mstar_dtv_tv);
             }
               break;
                   }
      case 3:
      {
        u4_count=file_header.dtv_atv_length;
          TCL_MSTAR_ATV_TV *mstar_atv_tv;
          TCL_MSTAR_DTV_TV *mstar_dtv_tv;
                  for(int i=0;i<u4_count;i++)
            {
              mstar_dtv_tv=new TCL_MSTAR_DTV_TV;
              Q_CHECK_PTR(mstar_dtv_tv);
              mstar_dtv_tv->Read_Data(in);
              q_mstar_dtv_tv<<mstar_dtv_tv;
              //q_mstar_dtv_tv.append(mstar_dtv_tv);
            }
          while(!in.atEnd())
            {
              mstar_atv_tv=new TCL_MSTAR_ATV_TV();
              Q_CHECK_PTR(mstar_atv_tv);
              mstar_atv_tv->Read_Data(in);
              q_mstar_atv_tv<<mstar_atv_tv;
              //q_mstar_atv_tv.apped(mstar_atv_tv);
            }
          break;
              }
       default:
       Q_ASSERT_X(dtv_atv_flag>=0&&dtv_atv_flag<4,"In TCL_USB::Read_MStar_Data() Line 317","dtv_atv_falg error!");
           //qDebug("MSTAR方案中DTV和ATV标志出现错误\n");
}


}
int TCL_USB::Write_MSTAR_Data(QDataStream &out,int dtv_atv_flag)
{
        switch(dtv_atv_flag)
        {

        case 0:
            TCL_MSTAR_ATV_TV *mstar_atv_tv;
            for(int i=0;i<q_mstar_atv_tv.size();i++)
            {
                mstar_atv_tv=q_mstar_atv_tv.at(i);
                Q_CHECK_PTR(mstar_atv_tv);
                mstar_atv_tv->Write_Data(out);
            }
            break;

        case 1:
            {
               TCL_MSTAR_DTV_TV *mstar_dtv_tv;
               for(int i=0;i<q_mstar_dtv_tv.size();i++)
               {
                   mstar_dtv_tv=q_mstar_dtv_tv.at(i);
                   Q_CHECK_PTR(mstar_dtv_tv);
                   mstar_dtv_tv->Write_Data(out);
               }
               break;
            }
        case 2:
            {
               TCL_MSTAR_ATV_TV *mstar_atv_tv;
               TCL_MSTAR_DTV_TV *mstar_dtv_tv;
               for(int i=0;i<q_mstar_atv_tv.size();i++)
               {
                mstar_atv_tv=q_mstar_atv_tv.at(i);
                Q_CHECK_PTR(mstar_atv_tv);
                mstar_atv_tv->Write_Data(out);
               }
               for(int i=0;i<q_mstar_dtv_tv.size();i++)
               {
                mstar_dtv_tv=q_mstar_dtv_tv.at(i);
                Q_CHECK_PTR(mstar_dtv_tv);
                mstar_dtv_tv->Write_Data(out);
               }
           }
            break;
        case 3:
            {
                TCL_MSTAR_DTV_TV *mstar_dtv_tv;
                TCL_MSTAR_ATV_TV *mstar_atv_tv;
                for(int i=0;i<q_mstar_dtv_tv.size();i++)
                {
                    mstar_dtv_tv=q_mstar_dtv_tv.at(i);
                    Q_CHECK_PTR(mstar_dtv_tv);
                    mstar_dtv_tv->Write_Data(out);
                }
                for(int i=0;i<q_mstar_atv_tv.size();i++)
                {
                    mstar_atv_tv=q_mstar_atv_tv.at(i);
                    Q_CHECK_PTR(mstar_atv_tv);
                    mstar_atv_tv->Write_Data(out);
                }
                break;
            }
            default:
             Q_ASSERT_X(dtv_atv_flag>=0&&dtv_atv_flag<4,"In TCL_USB::Write_MStar_Data() Line 386","dtv_atv_falg error!");
            //qDebug("MSTAR方案的DTV和ATV标志错误\n");
        }
}

int TCL_USB::Read_File(QDataStream &in)
{
    empty++;
    //文件头处理
        file_header=BIN_FILE_HEADER();
        char *magic_s;//;=new char[8];
        in>>magic_s;
        in>>file_header.flag
        >>file_header.dtv_atv_length;
        memcpy(file_header.magic,magic_s,8);
        delete magic_s;
        if(!Bin_Header_Test(file_header))
      {
        qDebug("File Magic Error\n");
        return 0;
      }

        solution=file_header.flag>>4;
        dtv_atv_flag=(file_header.flag&0x0f)>>2;
        version=file_header.flag&0x03;
   //头文件标志识别
   switch(solution)
   {
   case 0://MTK
       {
     if(Read_MTK_Data(in,dtv_atv_flag))
           {
             read_info=true;
             return 1;
           }
     else
         {
             read_info=false;
       return 0;
         }
       }
   case 1://MSTAR
       {
     if(Read_MSTAR_Data(in,dtv_atv_flag))
       return 1;
     else
       return 0;
        }
   default:
       Q_ASSERT_X(solution>1,"In TCL_USB::Read_File() Line 434 ","solution >1,This software ,now,just support mtk and mstar");

   }
   return 1;

}


int TCL_USB::Write_File(QDataStream &out)
 {
   // file_name="clone_edit.bin";
   const char *tmp=(const char *)file_header.magic;
     out<<tmp;
    out<<file_header.flag
      <<file_header.dtv_atv_length;


    switch(solution)
      {
      case 0:
    {
          if(Write_MTK_Data(out,dtv_atv_flag))
             write_info=1;
         else
             write_info=0;
         break;
    }
    case 1:
        {
              if(Write_MSTAR_Data(out,dtv_atv_flag))
                 write_info=1;
              else
                   write_info=0;
            break;
          }
        default:
        {
        Q_ASSERT_X(solution>1,"In TCL_USB::Write_File() Line 473 ","solution >1,This software ,now,just support mtk and mstar");

                  write_info= 0;
        }
        return write_info;
      }
 }


void TCL_USB::Del_ATV_Row(int list)
{
    Q_ASSERT(list<256&&list>=0);
    q_mstar_atv_tv.at(list)->ui2_ch_val=0;
    q_mstar_atv_tv.at(list)->ui2_isskip=1;
}

//modify standard files
void TCL_USB::set_mtk_atv(int row,int col, QVariant value)
{
    Q_ASSERT(row<q_mtk_atv_tv.size()&&col<5);
    q_mtk_atv_tv[row]->Set_Data(col,value);
}
void TCL_USB::set_mtk_dtv(int row,int col, QVariant value)
{
    Q_ASSERT(row<q_mtk_dtv_tv.size()&&col<15);
    q_mtk_dtv_tv[row]->Set_Data(col,value);
}
void TCL_USB::set_mstar_atv(int row,int col,QVariant value)
{
    Q_ASSERT(row<q_mstar_dtv_tv.size()&&col<5);
    q_mstar_atv_tv[row]->Set_Data(col,value);
}
void TCL_USB::set_mstar_dtv(int row,int col,QVariant value)
{
    Q_ASSERT(row<q_mstar_dtv_tv.size()&&col<15);
    q_mstar_dtv_tv[row]->Set_Data(col,value);
}

void TCL_USB::Clear_MTK_ATV()
{

    qDeleteAll(q_mtk_atv_tv.begin(),q_mtk_atv_tv.end());
    q_mtk_atv_tv.clear();
}

void TCL_USB::Clear_MTK_DTV()
{
    qDeleteAll(q_mtk_dtv_tv);
    q_mtk_dtv_tv.clear();
}

void TCL_USB::Clear_MSTAR_ATV()
{
    qDeleteAll(q_mstar_atv_tv);
  q_mstar_atv_tv.clear();
}

void TCL_USB::Clear_MSTAR_DTV()
{
    qDeleteAll(q_mstar_dtv_tv);
   q_mstar_dtv_tv.clear();
}

int TCL_USB::Get_Solution()
{
    return (int)solution;
}

